﻿using LinqToAmazonSL.AWS;
using System.Collections.Generic;

namespace LinqToAmazonSL
{
    public class AmazonDVDQueryCriteria : AmazonQueryCriteria
    {
        public AmazonDVDQueryCriteria()
        {
            this.SearchIndex = SearchIndex.DVD;
            Parameters = new Dictionary<string, QueryValue>();
            //Initialization of the stack where we save the operations to perform to the query
            OperationsStack = new Stack<Operations>();
        }

        //Generation of search criteria to dvd
        public override ItemSearchRequest[] ToAmazonRequest()
        {
            ItemSearchRequest[] requests = new ItemSearchRequest[2];
          
                requests[0] = new ItemSearchRequest() { SearchIndex = "DVD" };

                if (Parameters.ContainsKey("Title"))
                    requests[0].Title = Parameters["Title"].Value;
                if (Parameters.ContainsKey("Actor"))
                    requests[0].Actor = Parameters["Actor"].Value;
                if (Parameters.ContainsKey("Director"))
                    requests[0].Director = Parameters["Director"].Value;
                if (Parameters.ContainsKey("Publisher"))
                    requests[0].Publisher = Parameters["Publisher"].Value;
                if (Parameters.ContainsKey("Keywords"))
                    requests[0].Keywords = Parameters["Keywords"].Value;
                if (Parameters.ContainsKey("Audience"))
                {
                    List<AudienceRating> audience= new List<AudienceRating>();
                    switch (Parameters["Audience"].Value)
                    {
                        case "FamilyViewing": 
                            audience.Add(AudienceRating.FamilyViewing);
                            break;
                        case "G":
                            audience.Add(AudienceRating.G);
                            break;
                        case "Item12":
                            audience.Add(AudienceRating.Item12);
                            break;
                        case "Item16":
                            audience.Add(AudienceRating.Item16);
                            break;
                        case "Item18":
                            audience.Add(AudienceRating.Item18);
                            break;
                        case "Item6":
                            audience.Add(AudienceRating.Item6);
                            break;
                        case "NC17":
                            audience.Add(AudienceRating.NC17);
                            break;
                        case "NR":
                            audience.Add(AudienceRating.NR);
                            break;
                        case "PG":
                            audience.Add(AudienceRating.PG);
                            break;
                        case "PG13":
                            audience.Add(AudienceRating.PG13);
                            break;
                        case "R":
                            audience.Add(AudienceRating.R);
                            break;
                        case "Unrated":
                            audience.Add(AudienceRating.Unrated);
                            break;
                    }
                    requests[0].AudienceRating = audience.ToArray();
                }
                if (Parameters.ContainsKey("MaximumPrice"))
                    requests[0].MaximumPrice = Parameters["MaximumPrice"].Value;
                if (Parameters.ContainsKey("MinimumPrice"))
                    requests[0].MinimumPrice = Parameters["MinimumPrice"].Value;
                if (Parameters.ContainsKey("OrderByDescending"))
                    if (Parameters["OrderByDescending"].Value.Equals("Price"))
                        requests[0].Sort = "-price";
                if (Parameters.ContainsKey("OrderBy"))
                    if (Parameters["OrderBy"].Value.Equals("Price"))
                        requests[0].Sort = "price";
                    else
                        if (Parameters["OrderBy"].Value.Equals("Title"))
                            requests[0].Sort = "titlerank";

                requests[0].ResponseGroup = new string[] { "Medium" };
                requests[0].ItemPage = "1";
            
            return requests;
        }

        //Cleanup criterion search
        public override AmazonQueryCriteria ClearCriteria()
        {
            return new AmazonDVDQueryCriteria();
        }
    }
}
